//
//  VRRiskReportViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/18.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRRiskReportViewController.h"
#import "VRAddRiskReportViewController.h"

#import "XNoDataView.h"
#import "VRRiskReportCell.h"

#import "VRRiskReportModel.h"


@interface VRRiskReportViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XNoDataView *nodataView;

@property (nonatomic, strong) NSMutableArray<VRRiskReportModel *> *modelArray;

@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation VRRiskReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"隐患报告";
    
    self.modelArray = [NSMutableArray array];
    
    [self setupSubView];
    [self setupNav];
    weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        strongify(self)
        [self getRiskReportListWithIsRemoveArray:YES];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        strongify(self)
//        [self getRiskReportListWithIsRemoveArray:NO];
//    }];
//    [self getRiskReportListWithIsRemoveArray:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getRiskReportListWithIsRemoveArray:YES];
}

- (void)setupNav
{
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 1;
    [rightBtn setTitle:@"添加" forState:UIControlStateNormal];
    [rightBtn setTitleColor:MAINColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = VRFont(16);
    [rightBtn addTarget:self action:@selector(onAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 44, 44);
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

- (void)setupSubView
{
    [self.view addSubview:self.tableView];
}

#pragma mark - networking
- (void)getRiskReportListWithIsRemoveArray:(BOOL)isRemoveArray
{
    if (isRemoveArray) {
        self.currentPage = 1;
        [self.modelArray removeAllObjects];
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
//    param[@"page"] = @(self.currentPage++);
    //    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetRiskRepotList parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            NSArray *array = [VRRiskReportModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.modelArray addObjectsFromArray:array];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.nodataView.hidden = self.modelArray.count > 0;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                
                self.tableView.mj_footer.hidden = self.modelArray.count == 0;
                if (array.count < 10){
//                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    [self.tableView.mj_footer endRefreshing];
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView.mj_header endRefreshing];
                [SVProgressHUD showErrorWithStatus:msg];
                [self.tableView.mj_footer endRefreshing];
                [hud hideAnimated:YES];
            });
        }
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        });
    }];
}

#pragma mark - action
- (void)onAddBtnClick:(UIButton *)sender
{
    VRAddRiskReportViewController *addVc = [[VRAddRiskReportViewController alloc] init];
    [self.navigationController pushViewController:addVc animated:YES];
}


#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100 + 80 + 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRRiskReportCell *cell = [VRRiskReportCell cellWith:tableView];
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    VRSafetyTestingDetailViewController *detailVc = [[VRSafetyTestingDetailViewController alloc] init];
//    if (self.currentSelectType == 0) {
//        detailVc.fID = self.modelArray0[indexPath.row].fID;
//    }else{
//        detailVc.fID = self.modelArray1[indexPath.row].fID;
//    }
//
//    [self.navigationController pushViewController:detailVc animated:YES];
}


#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _tableView.tableFooterView = view1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.01;
        [self.view addSubview:self.tableView];
        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.bottom.mas_equalTo(self.view);
        //            make.top.mas_equalTo(0);
        //        }];
    }
    return _tableView;
}

- (XNoDataView *)nodataView
{
    if (_nodataView == nil) {
        
        _nodataView = [[XNoDataView alloc] init];
        _nodataView.hidden = YES;
        _nodataView.showText = @"暂无历史任务记录~";
        _nodataView.frame = CGRectMake(0, kScreenHeight * 0.5 - 200, kScreenWidth, 200);
        [self.tableView addSubview:_nodataView];
    }
    return _nodataView;
}

@end
